//! trait 定义模块
//!
//! 包含各种 trait 的定义和实现示例
//!
//! ## 知识点总结
//!
//! | Trait | 用途 | 关键方法 |
//! |-------|------|---------|
//! | HelloMacro | 宏实现示例 | hello_macro() |
//! | Summary | 摘要功能 | summarize() |
//! | Draw | 绘图功能 | draw() |

/// 一个简单的 trait 示例
pub trait HelloMacro {
    fn hello_macro();
}

/// 摘要 trait，用于为不同类型提供摘要功能
pub trait Summary {
    fn summarize(&self) -> String;

    /// 提供默认实现的 trait 方法
    fn summarize_author(&self) -> String {
        format!("(Read more from {}...)", self.summarize())
    }
}

/// 绘图 trait，用于为不同类型的图形提供绘制功能
pub trait Draw {
    fn draw(&self);
}

/// 新闻文章结构体
#[derive(Debug)]
pub struct NewsArticle {
    pub headline: String,
    pub location: String,
    pub author: String,
    pub content: String,
}

impl Summary for NewsArticle {
    fn summarize(&self) -> String {
        format!("{}, by {} ({})", self.headline, self.author, self.location)
    }
}

/// 推文结构体
#[derive(Debug)]
pub struct Tweet {
    pub username: String,
    pub content: String,
    pub reply: bool,
    pub retweet: bool,
}

impl Summary for Tweet {
    fn summarize(&self) -> String {
        format!("{}: {}", self.username, self.content)
    }

    fn summarize_author(&self) -> String {
        format!("@{}", self.username)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_news_article_summary() {
        let article = NewsArticle {
            headline: String::from("Penguins win the Stanley Cup Championship!"),
            location: String::from("Pittsburgh, PA, USA"),
            author: String::from("Iceburgh"),
            content: String::from(
                "The Pittsburgh Penguins once again are the best
            hockey team in the NHL.",
            ),
        };

        assert_eq!(article.summarize(), "Penguins win the Stanley Cup Championship!, by Iceburgh (Pittsburgh, PA, USA)");
    }

    #[test]
    fn test_tweet_summary() {
        let tweet = Tweet {
            username: String::from("horse_ebooks"),
            content: String::from("of course, as you probably already know, people"),
            reply: false,
            retweet: false,
        };

        assert_eq!(tweet.summarize(), "horse_ebooks: of course, as you probably already know, people");

        assert_eq!(tweet.summarize_author(), "@horse_ebooks");
    }
}
